package com.elong.businesstravel.modules.login.baseactivity;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

import com.android.support.jhf.edittextclear.EditTextClearView;
import com.android.support.jhf.handlerui.HandlerPostUI;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.network.NetworkClient;
import com.android.support.jhf.network.loopj.android.http.ResponseHandlerInterface;
import com.android.support.jhf.onClick.OnClickNetworkStatusListener;
import com.android.support.jhf.utils.ValidateOperator;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.baseactivity.BaseActivity;
import com.elong.businesstravel.base.utils.AppUserId;
import com.elong.businesstravel.base.utils.SaveUserOperate;
import com.elong.businesstravel.base.view.TitleNavBarView;
import com.elong.businesstravel.entitys.MemberLogin;
import com.elong.businesstravel.protocol.login.GetYZMRequest;
import com.elong.businesstravel.protocol.login.GetYZMResponse;
import com.elong.businesstravel.protocol.login.MemberCheckLoginRequest;
import com.elong.businesstravel.protocol.login.MemberCheckLoginResponse;

/**
 * 登陆基类
 * 
 * @author lenovo
 * 
 */
public class BaseLoginActivity extends BaseActivity {

	protected EditTextClearView mPhoneEditTextClearView;
	protected EditTextClearView mYzmEditTextClearView;
	protected Button mGetYzmButton;

	protected MemberLogin mMemberLogin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	protected void init() {

		mPhoneEditTextClearView = (EditTextClearView) findViewById(R.id.phoneNumberEditTextClearView);
		mPhoneEditTextClearView.getEditText().setInputType(
				InputType.TYPE_CLASS_PHONE);
		String phoneNumberString = SaveUserOperate.getLoginPhone(mContext);
		if (!phoneNumberString.equals("")) {
			mPhoneEditTextClearView.getEditText().setText(phoneNumberString);
		}
		mYzmEditTextClearView = (EditTextClearView) findViewById(R.id.yzmEditTextClearView);
		mYzmEditTextClearView.getEditText().setInputType(
				InputType.TYPE_CLASS_NUMBER);

		mGetYzmButton = (Button) findViewById(R.id.getYzmButton);

	}

	@Override
	protected void setListener() {
		mGetYzmButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				StringBuilder stringBuilder = new StringBuilder();
				if (checkYZM(stringBuilder)) {
					SaveUserOperate.setLoginPhone(mContext,
							mPhoneEditTextClearView.getEditText().getText()
									.toString());
					getYZMRequest();
					mYzmEditTextClearView.getEditText().requestFocus();
				} else {
					HandlerToastUI.getHandlerToastUI(mContext,
							stringBuilder.toString());
				}
			}
		});
	}

	protected void setTitleNavBar(String titleString,
			final OnClickListener onClickListener) {
		final TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage(titleString);
		titleNavBarView.setLeftDrawableImage(R.drawable.btn_title_back);
		titleNavBarView.setLeftButtonImageString(0, "返回", R.color.text_blue);
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (null != onClickListener) {
					onClickListener.onClick(v);
				} else {
					finish();
				}
			}
		});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	}

	protected boolean checkStartUse(StringBuilder errorStringBuilder) {
		errorStringBuilder.delete(0, errorStringBuilder.length());

		if (TextUtils.isEmpty(mPhoneEditTextClearView.getEditText().getText()
				.toString())) {
			errorStringBuilder.append("请输入手机号码");
			return false;
		} else if (!ValidateOperator.isPhoneNumberValid(mPhoneEditTextClearView
				.getEditText().getText().toString())) {
			errorStringBuilder.append("请输入正确的手机号");
			return false;
		} else if (TextUtils.isEmpty(mYzmEditTextClearView.getEditText()
				.getText().toString())) {
			errorStringBuilder.append("请输入验证码");
			return false;
		} else if (4 != mYzmEditTextClearView.getEditText().getText()
				.toString().length()) {
			errorStringBuilder.append("请输入正确的验证码");
			return false;
		}

		return true;
	}

	protected boolean checkYZM(StringBuilder errorStringBuilder) {
		if (TextUtils.isEmpty(mPhoneEditTextClearView.getEditText().getText()
				.toString())) {
			errorStringBuilder.append("请输入手机号码");
			return false;
		} else if (!ValidateOperator.isPhoneNumberValid(mPhoneEditTextClearView
				.getEditText().getText().toString())) {
			errorStringBuilder.append("请输入正确的手机号");
			return false;
		}
		return true;
	}

	/**
	 * 从新获取验证码
	 */
	protected void againGetYzmRequest() {
		final ScheduledExecutorService scheduledExecutorService = Executors
				.newScheduledThreadPool(1);
		scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
			private int timeCount = 60;

			@Override
			public void run() {
				HandlerPostUI.getHandlerPostUI(new Runnable() {

					@Override
					public void run() {
						timeCount--;
						if (0 == timeCount) {
							mGetYzmButton.setText("获取验证码");
							scheduledExecutorService.shutdownNow();
							mGetYzmButton.setEnabled(true);
						} else {
							mGetYzmButton.setEnabled(false);
							mGetYzmButton.setText("重发(" + timeCount + ")");
						}
					}
				});
			}
		}, 1 * 1000, 1 * 1000, TimeUnit.MILLISECONDS);
	}

	/**
	 * 获取手机验证码
	 */
	protected void getYZMRequest() {
		NetworkClient.getNetworkClient().PostRequest(
				new GetYZMRequest(mContext, mPhoneEditTextClearView
						.getEditText().getText().toString()),
				new GetYZMResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						HandlerToastUI.getHandlerToastUI(mContext,
								messageString);
						againGetYzmRequest();
					}
				});
	}
}
